package com.sinven.action;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sinven.entity.Comment;
import com.sinven.service.CommentService;
import com.sinven.util.GetUtil;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
public class CommentAction extends ActionSupport {
    @Autowired
    @Qualifier("CommentService")
    private CommentService commentService;
    private Comment comment;

    public Comment getComment() {
        return comment;
    }

    public void setComment(Comment comment) {
        this.comment = comment;
    }

    @Action(value = "listComment",results = {@Result(type = "json")})//返回的数据格式为json
    //根据商品Id查询
    public String ListComment() throws IOException {
        PrintWriter out= GetUtil.getOut();
        List<Comment> comments = commentService.selectCommentBygoodsId(comment.getGoodsId());
        String jsonString= JSON.toJSONString(comments);
        out.print(jsonString);
        for (Comment comm:comments){
            System.out.println(comm);
        }
        return NONE;
    }
    @Action(value = "comment",results = {@Result(type = "json")})//返回的数据格式为json
    public String comment()throws IOException{
        PrintWriter out= GetUtil.getOut();
        List<Comment> comments = commentService.selectCommentByCommentId(comment.getId());
        String jsonString= JSON.toJSONString(comments);
        out.print(jsonString);
        for (Comment comm:comments){
            System.out.println(comm);
        }
        return NONE;
    };

    @Action(value = "addComment",results = {@Result(type = "json")})//返回的数据格式为json
    public String addComment()throws IOException{
        PrintWriter out= GetUtil.getOut();
        int integer = commentService.addComment(comment);
        String jsonString= JSON.toJSONString(integer);
        if (integer>0) {
            out.print(jsonString);
        }
        return NONE;
    };
}
